﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;

namespace Jim.OA.Service;


/// <summary>
/// 单点登录
/// </summary>
/// <remarks>返回单点登录地址</remarks>        
[ApiDescriptionSettings(ApplicationConst.GroupName)]
public  class OASsoService : IDynamicApiController, ITransient
{
    private readonly OAClientFactory _oaClientFactory;
    private readonly UserManager _userManager;

    public OASsoService(OAClientFactory oaClientFactory, UserManager userManager)
    {
        _oaClientFactory = oaClientFactory;
        _userManager = userManager;
    }

    /// <summary>
    /// 获取单点登录结果
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>     
    [AllowAnonymous, NonUnify, HttpGet]
    public async Task<OAGetSsoTokenOutput> OAGetSsoToken([FromQuery]OAGetSsoTokenInput input)
    {
        var client = _oaClientFactory.CreateSsoClient();
        input.AppKey = client.AppKey;
        var result = await client.Execute(input);
        return result;
    }

    /// <summary>
    /// 获取SSO登录Url
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [AllowAnonymous, HttpGet]
    public async Task<string> OASsoLogin([FromQuery]OAGetSsoTokenInput input)
    {
        var client = _oaClientFactory.CreateSsoClient();
        input.AppKey = client.AppKey;
        var result = await client.Execute(input);


        //return new RedirectResult($"{client.BaseUrl}/wui/index.html?ssoToken={result.Data}#/main");
        return  $"{client.ServerUrl}/wui/index.html?ssoToken={result.Result}#/main";
    }
}
